using DA.AnimGraph;
using UnityEditor.UIElements;
using UnityEngine;

namespace DA.AnimGraph.Editor {

    internal class GraphPane : ContextPaneBase {

        HandleKeeper<AGAHandle_Graph> graphHK;

        ToolbarBreadcrumbs naviBar;
        GraphCanvasBase graphCanvas;
        public AUID uuid => graphHK.uuid;

        public GraphPane(ContextPaneBase parent) : base(parent) {
            naviBar = new ToolbarBreadcrumbs() {
                name = "navi_bar"
            };
        }

        protected override void OnSetHandle(AGAHandleBase handle) {
            Debug.Assert(handle is AGAHandle_Graph || handle == null);
            if (handle == null) {
                graphHK = default;
                return;
            }
            graphHK = new HandleKeeper<AGAHandle_Graph>(handle as AGAHandle_Graph);
        }

        protected override void OnClearUI() {
            graphCanvas?.Clear();
            Clear();
        }

        protected override void OnDrawUI() {
            //Add(naviBar); // todo:先不显示了
            naviBar.Clear();
            naviBar.PushItem(graphHK.handle.name); // todo: 先添加了graph再添加subgraph，rootsubgraph可改为和graph同名，然后不显示graph。graph的名字在这里不能点，有点多余
            BuildNaviBar();

            // graphCanvas
            if (graphCanvas != null) {
                graphCanvas.SetHandle(null);
                graphCanvas = null;
            }
            graphCanvas = new BlendTreeCanvas(this) {
                name = "graph_canvas",
                style = {
                    flexGrow = 1,
                }
            };
            Add(graphCanvas);
            graphCanvas.SetHandleFromParent(graphHK.handle.rootSubGraph);
        }

        void BuildNaviBar() {
            var rootSubGraph = graphHK.handle.GetChildHandle(graphHK.handle.rootSubGraph) as AGAHandle_SubGraph;
            BuildNaviBar(rootSubGraph);
        }

        void BuildNaviBar(AGAHandle_SubGraph handle) {
            // todo: subgraph之间要建立应用关系，这里递归地构建导航栏。目前没有subgraph，先省略
            // 点击功能也省略
            naviBar.PushItem(handle.name, () => { });
        }
    }
}
